package com.fl.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fl.model.TStudy;
import com.fl.service.StudyService;

import com.fl.util.ResultWeb;
import com.github.pagehelper.PageInfo;


@CrossOrigin
@RestController
@RequestMapping("/study")
public class StudyController extends BaseController{
	

	
	@Autowired
	StudyService StudyService;
	
	@RequestMapping("queryall")
	private @ResponseBody PageInfo<TStudy> queryactioninstance(HttpServletRequest request) {
		String datestartstr=this.getParameter(request, "datestartstr");
		String dateendstr=this.getParameter(request, "dateendstr");
		int pagenum=this.getPageNum(request);
		int pagesize=this.getPageSize(request);
		PageInfo<TStudy> list=this.StudyService.getPageInfo(pagenum, pagesize,datestartstr,dateendstr);
		return list;
	}
	
	
	@RequestMapping("insert")
	private @ResponseBody boolean insert(HttpServletRequest request) {
	
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		TStudy model=new TStudy();
		model.setFdate(jsonObject.getString("fdatestr"));
		model.setFstate(jsonObject.getInteger("fstateint"));
		model.setFimageurl(jsonObject.getString("fimageurl"));
		model.setFid("111");
		StudyService.TStudyMapper.insert(model);
		return true;
	}
	
	@RequestMapping("update")
	private @ResponseBody boolean update(HttpServletRequest request) {
	
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		TStudy model=new TStudy();
		model.setFdate(jsonObject.getString("fdatestr"));
		model.setFstate(jsonObject.getInteger("fstateint"));
		model.setFimageurl(jsonObject.getString("fimageurl"));
		model.setFid(jsonObject.getString("fid"));
		StudyService.TStudyMapper.updateByPrimaryKeySelective(model);
		return true;
	}
	@RequestMapping("delete")
	private @ResponseBody boolean delete(HttpServletRequest request) {
	
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		TStudy model=new TStudy();
		model.setFdate(jsonObject.getString("fdatestr"));
		model.setFstate(jsonObject.getInteger("fstateint"));
		model.setFimageurl(jsonObject.getString("fimageurl"));
		model.setFid(jsonObject.getString("fid"));
		StudyService.TStudyMapper.deleteByPrimaryKey(model.getFid());
		return true;
	}	
	
	@RequestMapping("queryinstance")
	private @ResponseBody ResultWeb<TStudy> queryinstance(HttpServletRequest request) {
		
		ResultWeb<TStudy> result=new ResultWeb<TStudy>();
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		String fid=jsonObject.getString("fid");
		TStudy model=this.StudyService.TStudyMapper.selectByPrimaryKey(fid);
		result.setInstance(model);
		result.setSuccess(model!=null);
		return result;
	}	
	
	@RequestMapping("chgstate")
	private @ResponseBody boolean chgstate(HttpServletRequest request) {
		
		//ResultWeb<TStudy> result=new ResultWeb<TStudy>();
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		String fid=jsonObject.getString("fid");
		TStudy model=this.StudyService.TStudyMapper.selectByPrimaryKey(fid);
		if(model.getFstate()==1) {
			model.setFstate(0);
		} else {
			model.setFstate(1);
		}
		this.StudyService.TStudyMapper.updateByPrimaryKeySelective(model);
		return true;
	}
	
	
	
	@RequestMapping("deleteselection")
	private @ResponseBody boolean deleteselection(HttpServletRequest request) {
		
		//ResultWeb<TStudy> result=new ResultWeb<TStudy>();
		com.alibaba.fastjson.JSONObject jsonObject=this.getJsonObject(request);
		String ids=jsonObject.getString("ids");
		com.alibaba.fastjson.JSONArray jsonarray=com.alibaba.fastjson.JSONArray.parseArray(ids);
		for(Object jo:jsonarray)
		{
			String key=jo.toString();
			this.StudyService.TStudyMapper.deleteByPrimaryKey(key);
		}
		return true;
	}

	
	
	
	
	
}
